package com.mf.cloud.controller;

import com.mf.cloud.apis.PayFeignApi;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lmf
 * @Create: 2024/3/17 16:28
 * Description:  舱壁隔离测试
 */
@RestController
@Tag(name="测试隔离接口")
public class OrderBulkheadController {
    @Resource
    private PayFeignApi payFeignApi;


    /**
     * 信号量舱壁
     * 连接 cloud-payment-service 服务  兜底方法 myCircuitFallback  类型 Bulkhead.Type.SEMAPHORE  信号量
     */
    @GetMapping("/feign/pay/bulkhead/{id}")
    @Operation(summary = "查询接口")
    @Bulkhead(name = "cloud-payment-service", fallbackMethod = "myCircuitFallback", type = Bulkhead.Type.SEMAPHORE)
    public String myBulkhead(@PathVariable("id") Integer id) {
        return payFeignApi.myBulkhead(id);
    }

    public String myCircuitFallback(Integer id, Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return "myBulkheadFallback，隔板超出（信号量舱壁）最大限制，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
    }


    /**
     * 固定线程池舱壁
     * 连接 cloud-payment-service 服务  兜底方法 myCircuitFallback  类型 Bulkhead.Type.SEMAPHORE  固定线程池舱壁
     */
    @GetMapping("/feign/pay/bulkhead2/{id}")
    @Operation(summary = "查询接口")
    @Bulkhead(name = "cloud-payment-service", fallbackMethod = "myCircuitFallback2", type = Bulkhead.Type.THREADPOOL)
    public CompletableFuture<String> myBulkhead2(@PathVariable("id") Integer id) {
        System.out.println(Thread.currentThread().getName() + "开始进入");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "结束");

        return CompletableFuture.supplyAsync(() -> payFeignApi.myBulkhead(id) + "\t Bulkhead.Type.THREADPOOL");

    }

    public CompletableFuture<String> myCircuitFallback2(Integer id, Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return CompletableFuture.supplyAsync(() -> "\t Bulkhead.Type.THREADPOOL 系统繁忙，（固定线程池舱壁）请稍后再试-----/(ㄒoㄒ)/~~");
    }

}
